package com.bite.friend.service.user.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.bite.common.core.constants.Constants;
import com.bite.common.core.domain.PageQueryDTO;
import com.bite.common.core.domain.TableDataInfo;
import com.bite.common.core.enums.ExamListType;
import com.bite.common.core.utils.ThreadLocalUtil;
import com.bite.friend.domain.exam.vo.ExamVO;
import com.bite.friend.domain.message.vo.MessageTextVO;
import com.bite.friend.manager.MessageCacheManager;
import com.bite.friend.mapper.message.MessageTextMapper;
import com.bite.friend.service.user.IUserMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserMessageServiceImpl implements IUserMessageService {

    @Autowired
    private MessageCacheManager messageCacheManager;

    @Autowired
    private MessageTextMapper messageTextMapper;

    @Override
    public TableDataInfo list(PageQueryDTO pageQueryDTO) {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID,Long.class);
        Long total = messageCacheManager.getListSize(userId);
        List<MessageTextVO> messageTextVOList;
        if (total == null || total <= 0){
            //查数据库
            PageHelper.startPage(pageQueryDTO.getPageNum(),pageQueryDTO.getPageSize());
            messageTextVOList = messageTextMapper.selectUserMsgList(userId);
            //将从数据库查到的数据插入缓存
            messageCacheManager.refreshCache(userId);
            total = new PageInfo<>(messageTextVOList).getTotal();
        } else {
            messageTextVOList = messageCacheManager.getMsgTextVOList(pageQueryDTO,userId);
        }
        if (CollectionUtil.isEmpty(messageTextVOList)){
            return TableDataInfo.empty();
        }
        return TableDataInfo.success(messageTextVOList,total);
    }
}
